package com.dm.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@Configuration

public class ThreadPoolConfig {

    private static final int CORE_POOL_SIZE=10;
    private static final int MAX_POOL_SIZE=50;


    private static final int QUEUE_CAPACITY=1000;
    /**
     Database
     ★线程池维护线程所允许的空闲时间
     */
    private static final int KEEP_ALIVE_SECONDS=500;

    @Bean("taskExecutor")
    public ExecutorService executorService(){
        AtomicInteger c=new AtomicInteger(1);
        LinkedBlockingQueue<Runnable> queue=new LinkedBlockingQueue<>(QUEUE_CAPACITY);
        return new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_SECONDS,
                TimeUnit.MILLISECONDS,
                queue,
                r->new Thread(r,"itheima-pool-"+c.getAndIncrement()),
        new ThreadPoolExecutor.DiscardPolicy()
        );

    }
}
